<?php

use common\models\c2\search\ProductSkuSearch;
use kzo\base\models\statics\OperationEvent;
use kzo\base\widgets\ui\bs4\Alert;
use kzo\base\widgets\ui\common\grid\GridView;
use yii\bootstrap\Modal;
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use yii\widgets\Pjax;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;

/** @var $model \backend\models\c2\form\EavSkuForm */
/** @var $this \yii\web\View */

$messageName = $model->getMessageName();
$searchModel = new ProductSkuSearch();
$searchModel->product_id = $model->entityModel->id;
$params = ArrayHelper::merge(['product_id' => $model->entityModel->id], Yii::$app->request->queryParams);
$dataProvider = $searchModel->search($params);
$attributes = $model->getEavFormAttributes();

?>

<?php Pjax::begin(['id' => $model->getPjaxName(), 'formSelector' => $model->getBaseFormName(true), 'enablePushState' => false]) ?>

<?php
$form = ActiveForm::begin([
    'action' => ['generate-skus'],
    'type' => ActiveForm::TYPE_HORIZONTAL,
    'options' => [
        'id' => $model->getBaseFormName(),
        'data-pjax' => 0,
    ],
    'formConfig' => [
        'labelSpan' => 4,
        'deviceSize' => ActiveForm::SIZE_SMALL
    ],
    'fieldConfig' => [
        // 'showLabels' => true,
        'showHints' => false,
        // 'template' => "{label}\n{hint}\n{input}\n{error}",
        'horizontalCssClasses' => [
            // 'label' => 'col-4',
            // 'offset' => 'col-sm-offset-2',
            // 'wrapper' => 'col-sm-4',
        ],
    ],
]);
?>

<div class="<?= $model->getPrefixName('form') ?>">

    <?php if (Yii::$app->session->hasFlash($messageName)): ?>
        <?php if (!$model->hasErrors()) {
            echo Alert::widget([
                'type' => Alert::TYPE_SUCCESS,
                'title' => 'Well Down',
                'icon' => 'fas fa-ok-circle',
                'body' => Yii::$app->session->getFlash($messageName),
                'showSeparator' => true,
                'delay' => 2000
            ]);
        } else {
            echo Alert::widget([
                'type' => Alert::TYPE_WARNING,
                'title' => 'Warning!',
                'icon' => 'fas fa-ok-circle',
                'body' => Yii::$app->session->getFlash($messageName),
                'showSeparator' => true,
            ]);
        }
        ?>
    <?php endif; ?>

    <div class="well">
        <?php
        if (count($attributes) > 0) {
            echo Form::widget([
                'model' => $model,
                'form' => $form,
                'columns' => 5,
                'attributes' => $attributes,
            ]);
            echo Html::hiddenInput('entity_id', $model->entityModel->id);

            echo Html::beginTag('div', ['style' => 'text-align: right', 'class' => 'mb-2']);
            echo Html::submitButton('<i class="fa fa-save"></i> ' . Yii::t('app.c2', 'Generate'), ['type' => 'button', 'class' => 'btn btn-warning pull-right']);
            echo Html::endTag('div');
        } else {
            echo Yii::t('app.c2', 'There are not available attributes for now!');
        }
        ?>
    </div>
</div>
<?php ActiveForm::end(); ?>

<div class="well">
    <?php
    echo GridView::widget([
        'id' => $model->getPrefixName('grid'),
        'dataProvider' => $dataProvider,
        // 'filterModel' => $searchModel,
        'pjax' => true,
        // 'pjaxSettings' => [
        //     'neverTimeout' => true,
        //     'options' => [
        //         'id'=>uniqid(),
        //         'enablePushState' => false,
        //         'clientOptions' => [
        //             'push' => false,
        //             'skipOuterContainers' => true,
        //            // 'replaceRedirect' => false
        //         ]
        //     ]
        // ],
        'hover' => true,
        'showPageSummary' => true,
        'panel' => ['type' => GridView::TYPE_PRIMARY, 'heading' => Yii::t('app.c2', 'Items'),],
        'toolbar' => [
            [
                'content' =>
                    Html::a('<i class="fas fa-redo"></i>', Url::current(), [
                        'class' => 'btn btn-outline-secondary',
                        'title' => Yii::t('app.c2', 'Reset Grid')
                    ]),
                'options' => ['class' => 'btn-group mr-2']
            ],
        ],
        'exportConfig' => [],
        'columns' => [
            ['class' => 'kartik\grid\CheckboxColumn'],
            ['class' => 'kartik\grid\SerialColumn'],
            // [
            //     'class' => 'kartik\grid\ExpandRowColumn',
            //     'expandIcon' => '<span class="fa fa-plus-square-o"></span>',
            //     'collapseIcon' => '<span class="fa fa-minus-square-o"></span>',
            //     'detailUrl' => Url::toRoute('detail'),
            //     'value' => function ($model, $key, $index, $column) {
            //         return GridView::ROW_COLLAPSED;
            //     },
            // ],
            // 'id',
            // 'product_id',
            // 'attr_params:ntext',
            // 'hash',
            // 'sku',
            // 'name',
            //
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'sku',
                'editableOptions' => [
                    'formOptions' => ['action' => Url::toRoute('sku-edit-column')],
                    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
                    'pjaxContainerId' => $model->getPjaxName(),
                ],
                'vAlign' => 'middle',
                'width' => '200px',
                'pageSummary' => false,
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'label',
                'editableOptions' => [
                    'formOptions' => ['action' => Url::toRoute('sku-edit-column')],
                    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
                    'pjaxContainerId' => $model->getPjaxName(),
                ],
                'vAlign' => 'middle',
                'width' => '200px',
                'pageSummary' => false,
            ],
            // 'code',

            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'factory_price',
                'editableOptions' => [
                    'formOptions' => ['action' => Url::toRoute('sku-edit-column')],
                    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
                    // 'inputType' => \kartik\editable\Editable::INPUT_MONEY,
                    'options' => ['pluginOptions' => ['min' => 0, 'max' => 1000000.0]],
                    'pjaxContainerId' => $model->getPjaxName(),
                ],
                'hAlign' => 'right',
                'vAlign' => 'middle',
                // 'width' => '100px',
                'format' => ['decimal', 2],
                'pageSummary' => true,
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'weight',
                'editableOptions' => [
                    'formOptions' => ['action' => Url::toRoute('sku-edit-column')],
                    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
                    // 'inputType' => \kartik\editable\Editable::INPUT_MONEY,
                    'options' => ['pluginOptions' => ['min' => 0, 'max' => 1000000.0]],
                    'pjaxContainerId' => $model->getPjaxName(),
                ],
                'hAlign' => 'right',
                'vAlign' => 'middle',
                // 'width' => '100px',
                'format' => ['decimal', 2],
                // 'pageSummary' => true,
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'unit',
                'editableOptions' => [
                    'formOptions' => ['action' => Url::toRoute('sku-edit-column')],
                    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
                    // 'inputType' => \kartik\editable\Editable::INPUT_MONEY,
                    'options' => ['pluginOptions' => ['min' => 0, 'max' => 1000000.0]],
                    'pjaxContainerId' => $model->getPjaxName(),
                ],
                'hAlign' => 'right',
                'vAlign' => 'middle',
                // 'width' => '100px',
                'format' => ['decimal', 2],
                // 'pageSummary' => true,
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'stock',
                'editableOptions' => [
                    'formOptions' => ['action' => Url::toRoute('sku-edit-column')],
                    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
                    'options' => ['pluginOptions' => ['min' => 0, 'max' => 500000]],
                    'pjaxContainerId' => $model->getPjaxName(),
                ],
                'hAlign' => 'right',
                'vAlign' => 'middle',
                // 'width' => '200px',
                'format' => ['integer'],
                // 'pageSummary' => true,
            ],
            [
                'attribute' => 'is_released',
                'class' => '\kartik\grid\EditableColumn',
                'editableOptions' => [
                    'inputType' => \kartik\editable\Editable::INPUT_DROPDOWN_LIST,
                    'formOptions' => ['action' => Url::toRoute('sku-edit-column')],
                    'data' => \common\models\c2\statics\WhetherType::getHashMap('id', 'label'),
                    'displayValueConfig' => \common\models\c2\statics\WhetherType::getHashMap('id', 'label'),
                    'pjaxContainerId' => $model->getPjaxName(),
                ],
                'filter' => \common\models\c2\statics\WhetherType::getHashMap('id', 'label'),
                'value' => function ($model) {
                    return $model->getStatusLabel();
                }
            ],
            [
                'class' => \kzo\base\widgets\ui\common\grid\ActionColumn::className(),
                'template' => '{assemble} {delete}',
                'width' => '200px',
                'urlCreator' => function ($action, $model, $key, $index) {
                    if ($action === 'delete') {
                        return Url::toRoute(['delete-sku', 'id' => $model->id]);
                    }
                },
                'buttons' => [
                    'assemble' => function ($url, $model, $key) {
                        return Html::a(Yii::t('app.c2', 'Product Assemble'), [
                            'assemble/sku-assemble-edit',
                            'id' => $model->id,
                            // 'type' => \common\models\c2\statics\AssembleType::TYPE_MATERIAL,
                        ], [
                            'title' => Yii::t('app.c2', 'Assemble'),
                            'aria-label' => Yii::t('app.c2', 'Assemble'),
                            'data-pjax' => '0',
                            'class' => 'assemble btn btn-primary btn-sm'
                        ]);
                    }
                ],
            ],
        ],
    ]);


    //    echo Html::beginTag('div', ['class' => 'box-footer']);
    //    echo Html::dropDownList('exchange', common\models\c2\statics\IsExchangeState::TYPE_EMPTY, common\models\c2\statics\IsExchangeState::getHashMap('id', 'label'), ['class' => 'is-exchange pull-right']);
    //    echo Html::endTag('div');
    ?>

</div>
<?php Pjax::end(); ?>

<?php


Modal::begin([
    'id' => 'sku-assemble-modal',
    'size' => 'modal-xl',
]);
Modal::end();

$js = "";

$js .= "jQuery(document).on('click', 'a.assemble', function(e) {
                e.preventDefault();
                jQuery('#sku-assemble-modal').modal('show').find('.modal-content').html('" . Yii::t('app.c2', 'Loading...') . "').load(jQuery(e.currentTarget).attr('href'));
            });";

$this->registerJs($js);

?>